package edu.cicese.alz.swing;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 27/06/12
 * Time: 05:40 PM
 */
public class MarkerPreview extends JButton {

	private Color color, borderColor;

	public MarkerPreview(Color color, Color borderColor, int width, int height) {
		super();
		this.color = color;
		this.borderColor = borderColor;
		setPreferredSize(new Dimension(width, height));
		setMaximumSize(getPreferredSize());
		setBorder(BorderFactory.createEmptyBorder());
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public void setBorderColor(Color borderColor) {
		this.borderColor = borderColor;
	}

	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
	    g2.setStroke(new BasicStroke());
		g2.clearRect(0, 0, getWidth(), getHeight());
        int radius = 5;
        int diameter = radius * 2;
		int x = getWidth() / 2;
		int y = getHeight() - 3;
	    Area areaOne = new Area(new Ellipse2D.Double(x - radius, y - radius - 8, diameter, diameter));
	    Polygon triangle = new Polygon();
	    triangle.addPoint(x - 5, y - 7);
	    triangle.addPoint(x + 5, y - 7);
	    triangle.addPoint(x, y);
	    Area areaTwo = new Area(triangle);
	    areaOne.add(areaTwo);
        g2.setColor(color);
	    g2.fill(areaOne);
        g2.setColor(borderColor);
	    g2.draw(areaOne);
	}
}
